<?php
/*
Template Name: Change Password
*/
if (!is_user_logged_in()){
wp_redirect(get_site_url());
}
global $current_user;
get_currentuserinfo();
$user_ID = get_current_user_id(); 
get_header();
$successid = isset( $_GET['success'] ) ? intval( $_GET['success'] ) : 0;
$success2 = "<span style='color:green'>Password Anda berhasil diubah!</span>";
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
$oldpassword= $_POST["oldpassword"];
$newpassword = $_POST["newpassword"];
$confirmpassword = $_POST["confirmpassword"];
//$user = get_user_by( 'login', $username );
$failed="";
$success2="";
if (empty ($oldpassword)){
$failed ="Password lama harus diisi! " ;
}
 if (empty ($newpassword)){
$failed ="Password baru harus diisi! ";
}
if (empty ($confirmpassword)){
$failed ="Konfirmasi Password baru harus diisi! ";
}
if ( !wp_check_password( $oldpassword, $current_user->user_pass, $user_ID) ){
   $failed = "Password lama tidak cocok! Silakan ulangi kembali. ";}
if ($oldpassword == $newpassword){
 $failed = "Password baru tidak boleh sama dengan password lama! ";
}
if ($newpassword !=$confirmpassword){
	$failed = "Password baru tidak sama dengan konfirmasi password baru! Silakan ulangi kembali. ";
}
if ($failed==""){
$user=array(
			'user_pass' => $newpassword,
			'ID' => $user_ID
	);
wp_update_user( $user);
wp_redirect(get_site_url()."/ubahpassword?success=1");
}

}
?>
<div class="col-md-12 column content">
	<div class="row clearfix">
		<?php get_template_part('menuprofil');?>
		<div class="col-md-9 column" > <!-- untuk kolom kedua yang dibagi 2 -->
				<div class="row clearfix">
					<div class="col-md-12 column">
					<br/>
					<h7> Ubah Password </h7> <?php if ($successid==1) echo $success2;
					echo "<span style='color:red'>".$failed. "</span>";?>
					<br/>
					<br/>
						<form method="post">
						<input class="form-control" type="password" placeholder="Password Lama" name="oldpassword" value="<?php if (!empty($oldpassword)) echo $oldpassword;?>" /><br/><br/>
						<input class="form-control" type="password" placeholder="Password Baru" name="newpassword" value="<?php if (!empty($newpassword)) echo $newpassword;?>"/><br/><br/>
						<input class="form-control" type="password" placeholder="Konfirmasi Password Baru" name="confirmpassword" value="<?php if (!empty($confirmpassword)) echo $confirmpassword;?>" />
						<br/>
						<br/>
						 <button type="submit" class="btn btn-sm" name="submit" >Simpan Perubahan</button>
                        <?php //wp_nonce_field( 'update-user' ) ?>
                        <input name="action" type="hidden" id="action" value="update-password" />
						</form>
					</div>
				</div>
		</div>
	</div>
</div>
<?php get_footer();?>